function $(A) {
    return document.getElementById(A)
}

function addEvent(id, evtType, func) {
    var elem = document.getElementById(id);
    if (elem === 'null') {
        return;
    }
    if (window.addEventListener) {
        elem.addEventListener(evtType, func, false);
    } else if (window.attachEvent) {
        elem.attachEvent("on" + evtType, func);
    } else {
        elem["on" + evtType] = func;
    }
}

function bind() {
    function handler(i) {

        return function (event) {
            var list = $('checkBoxLi' + i);
            var link = $('checkBoxLink' + i);
            if (event.target.tagName === 'A') {
                if (!link.classList.contains('check')) {
                    list.classList.add('check');
                }
                window.open(link.getAttribute('rel'), "_blank",
                    "-mx-active=no");
            } else if (event.target.tagName === 'SPAN') {
                list.classList.toggle('check');
            }
        };
    }

    var pageList = document.getElementById('pageList');
    for (var i = 0; i < pageList.children.length; i++) {
        addEvent('checkBoxLi' + i, 'click', handler(i));
    }
}

function isCheck(i) {
    return $('checkBoxLi' + i).classList.contains('check');
}

// 全部打开 按钮对应事件
function openPages() {
    var count = document.getElementById('pageList').children.length;
    var countOpen = 0;

    // 检查有多少页面需要打开
    for (var i = 0; i < count; i++) {
        if (!isCheck(i)) {
            countOpen++;
        }
    }

    // 警告打开过量页面
    if (countOpen > 30) {
        if (!confirm(countOpen + '个链接将会被打开，这有可能使浏览器卡死。确定继续吗？')) {
            return; //不执行打开页面操作
        }
    }

    // 执行打开页面操作
    for (var i = 0; i < count; i++) {
        var list = $('checkBoxLi' + i);
        if (!isCheck(i)) {
            var url = $('checkBoxLink' + i).getAttribute('rel');
            window.open(url, "_blank", "-mx-active=no");
        }
    }
}

bind();
addEvent('openPages', 'click', openPages);